package com.boot.main.service.impl;

import com.boot.main.entity.BcomArticle;
import com.boot.main.entity.BcomCategory;
import com.boot.main.entity.BcomTag;
import com.boot.main.mapper.BcomArticleMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IArticleService;
import com.boot.main.utils.IsNotEmptyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService implements IArticleService {
    @Resource
    private BcomArticleMapper articleMapper;

    @Override
    public ResponseData getArticles(Integer page, Integer limit, String title, Long cid){
        List<BcomArticle> items = articleMapper.getArticles((page-1)*limit, limit, title, cid);
        Integer count = articleMapper.getTotalCount(title, cid);
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取文章成功", map);
    }

    @Override
    public ResponseData getBlogArticles(Integer page, Integer limit, String title, Long cid, String tag){
        List<BcomArticle> items = articleMapper.getBlogArticles((page-1)*limit, limit, title, cid, tag);
        for(BcomArticle item:items){
            item.setContent(item.getContent().substring(0, Math.round(item.getContent().length()/2)));
        }
        Integer count = articleMapper.getTotalCount(title, cid);
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取文章成功", map);
    }

    @Override
    public ResponseData getArticleById(Long id){
        BcomArticle items = articleMapper.getArticleById(id);
        Integer view = items.getView();
        articleMapper.updateArticleView(view + 1, id);
        return ResponseDataUtil.success("获取文章成功", items);
    }

    @Override
    public ResponseData getTags(){
//        Long userId = getUserId();
        List<BcomTag> items = articleMapper.getTags();
        return ResponseDataUtil.success("获取文章成功", items);
    }

    @Override
    public ResponseData saveArticle(BcomArticle article){
        article.setCreate_time(new Date().getTime());
        article.setUpdate_time(new Date().getTime());
        article.setView(0);
        Integer num = articleMapper.saveArticle(article);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("保存文章失败", null);
        }
        return ResponseDataUtil.success("保存文章成功", null);
    }

    @Override
    public ResponseData updateArticle(BcomArticle article){
        article.setUpdate_time(new Date().getTime());
        Integer num = articleMapper.updateArticle(article);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("修改文章失败", null);
        }
        return ResponseDataUtil.success("修改文章成功", null);
    }

    @Override
    public ResponseData deleteArticle(Long id){
        Integer num = articleMapper.deleteArticle(id);
        return ResponseDataUtil.success("删除文章成功", null);
    }
}
